<?php

/**
 * This is the model class for table "{{member_collect}}".
 *
 *	所有的资料，
 *	某个会员的总数，，，，某个商品的总数，
 *	删除，，，添加。。。。。
 *	
 *
 *
 * 
 * The followings are the available columns in table '{{member_collect}}':
 * @property integer $CollectID
 * @property integer $GoodsID
 * @property string $GoodsName
 * @property string $Pprice
 * @property string $Image
 * @property integer $AddTime
 * @property integer $UserID
 */
class MemberCollectModel extends BaseModel
{

	const KEY_HASH_LIST = 'membercollect_hash'; //用redis存储hash数据的key

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{member_collect}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('Image', 'required'),
			array('GoodsID, AddTime, UserID', 'numerical', 'integerOnly'=>true),
			array('GoodsName', 'length', 'max'=>500),
			array('Pprice', 'length', 'max'=>10),
			array('Image', 'length', 'max'=>255),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('CollectID, GoodsID, GoodsName, Pprice, Image, AddTime, UserID', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'CollectID' => 'Collect',
			'GoodsID' => 'Goods',
			'GoodsName' => 'Goods Name',
			'Pprice' => 'Pprice',
			'Image' => 'Image',
			'AddTime' => 'Add Time',
			'UserID' => 'User',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('CollectID',$this->CollectID);
		$criteria->compare('GoodsID',$this->GoodsID);
		$criteria->compare('GoodsName',$this->GoodsName,true);
		$criteria->compare('Pprice',$this->Pprice,true);
		$criteria->compare('Image',$this->Image,true);
		$criteria->compare('AddTime',$this->AddTime);
		$criteria->compare('UserID',$this->UserID);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	public function createSearchCriteria()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('CollectID',$this->CollectID);
		$criteria->compare('GoodsID',$this->GoodsID);
		$criteria->compare('GoodsName',$this->GoodsName,true);
		$criteria->compare('Pprice',$this->Pprice,true);
		$criteria->compare('Image',$this->Image,true);
		$criteria->compare('AddTime',$this->AddTime);
		$criteria->compare('UserID',$this->UserID);

		return $criteria;
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return MemberCollectModel the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * 取所有的数据然后缓存，其它的函数都是先通过这个函数来取，然后再处理
	 * @param  [type] $intCatID [description]
	 * @return [type]           [description]
	 */
	public static function getList(){
		$redis = RedisCluster::getInstance()->getRedis();
		$arrResult = array();
		$datasKey = $redis->hKeys(self::KEY_HASH_LIST);//取得所有的key
		foreach ($datasKey as $key => $value) {
			$d = explode('_', $value);
			$arrResult[$key]['UserID'] = $d[0];
			$arrResult[$key]['GoodsID'] = $d[1];
			$arrResult[$key]['AddTime'] = $redis->hGet(self::KEY_HASH_LIST, $value);
		}
		return $arrResult;
	}

	/**
	 * 批量删除
	 * @return [type] [description]
	 */
	public static function deleteList($data = array(), $uid){
		if(!$data){
			return false;
		}
		$redis = RedisCluster::getInstance()->getRedis();
		foreach ($data as $key => $value) {		
			$keyCollect = $uid . '_' . $value;
			$redis->hDel(self::KEY_HASH_LIST, $keyCollect);
		}
		return true;		
	}
	/**
	 * 获取用户的收藏数
	 * @param  [type] $UserID [description]
	 * @return [type]         [description]
	 */
	public static function getUserCount($UserID){
		$UserID = empty($UserID) ? yii::app()->user->id : $UserID;
		$datas = self::getList();
		$count = 0;
		foreach ($datas as $data) {
			if($data['UserID'] == $UserID){
				$count++;
			}
		}
		return $count;
	}
	/**
	 * 获取用户的收藏数
	 * @param  [type] $UserID [description]
	 * @return [type]         [description]
	 */
	public static function getUser($UserID){
		$UserID = empty($UserID) ? yii::app()->user->id : $UserID;
		$datas = self::getList();
		$count = 0;
		$arrResult = array();
		foreach ($datas as $data) {
			if($data['UserID'] == $UserID){
				$arrResult[$data['GoodsID']] = $data;
			}
		}
		return $arrResult;
	}
	/**
	 * 获取商品的收藏数
	 * @param  [type] $UserID [description]
	 * @return [type]         [description]
	 */
	public static function getGoodsCount($GoodsID){
		$datas = self::getList();
		$count = 0;
		foreach ($datas as $data) {
			if($data['GoodsID'] == $GoodsID){
				$count++;
			}
		}
		return $count;
	}

	/**
	 * 用户收藏商品
	 * key为membercollect_hash，，，主键为{$userID}_{$goodsID}  ,, 值为TIME_TIME;
	 * @param  [type] $goodsID [description]
	 * @param  [type] $userID  [description]
	 * @return [type]          [description]
	 */
	public static function collect($goodsID,$userID){
        $redis = RedisCluster::getInstance()->getRedis();
        $redis->hSetNx(self::KEY_HASH_LIST, "{$userID}_{$goodsID}", TIME_TIME);
        return self::getGoodsCount($goodsID);          	       		
	}
}
